package de.gameduell.framework.gui.behavior {
	import flash.display.DisplayObject;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.gui.behavior.IBehavior;
	import de.gameduell.framework.gui.wrapper.SpriteWrapper;	

	/**
	 * @author GD
	 */
	public class SelectBehavior extends EventDispatcher implements IBehavior {
		
		public static const SELECTED:String   = "selected";
		public static const UNSELECTED:String = "unselected";
		
		private var _sprite:SpriteWrapper;
		private var _selected:Boolean;
		private var _active:Boolean;
		private var _highlight:DisplayObject;
		
		public function init(sprite:SpriteWrapper):void {
			_sprite    = sprite;
			_selected  = false;
			_active    = true;
			_highlight = _sprite.getChildByName("highlight");
			_highlight.visible = false;
			_sprite.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		
		private function mouseUp(event:MouseEvent):void {
			if(_active){
				_selected = !_selected;
				dispatchEvent(new BehaviorEvent(_selected ? SELECTED : UNSELECTED, _sprite.name));
				
				try{
					_sprite.getChildByName("highlight").visible = _selected; 
				}catch(e:Error){
					Debug.trace("SelectBehavior::mouseUp "+e,Debug.ERROR);
				}
			}
		}

		public function set active(isActive:Boolean):void {
			_active = isActive;
		}
		
		public function get active():Boolean{
			return _active;
		}
		
		public function getEventTypes():Array {
			return new Array(SELECTED,UNSELECTED);
		}
	}
}
